﻿@using AntDesign.JsInterop
@using AntDesign.Docs.Services
@inherits ComponentBase

<div class="code-box" id="@DemoId">
    <section class="code-box-demo">
        @if (Demo.Iframe > 0)
        {
            <MockBrowser Height="@Demo.Iframe.Value" WithUrl="@($"/mock?demoId={DemoId}&type={Demo.Type}")" />
        }
        else
        {
            @Component
        }
    </section>
    <section class="code-box-meta markdown">
        <a class="code-box-title" @onclick="@AnchorClick" >
            @Demo.Title
        </a>
        <div class="code-box-description">
            @((MarkupString)Demo.Description)
        </div>
        <div class="code-box-actions">
            <Tooltip Title="@lang.Resources[_copied?"app.demo.copied":"app.demo.copy"]" OnVisibleChange="() => _copied = false">
                <Unbound>
                    <Icon RefBack="context" Type="@(_copied?"check" : "snippets")" Class="code-box-code-copy code-box-code-action" OnClick="Copy" />
                </Unbound>
            </Tooltip>
            <Tooltip Title="@(codeExpand?lang.Resources["app.demo.code.hide"] : lang.Resources["app.demo.code.show"])">
                <Unbound>
                    <span @ref="context.Current" class="code-expand-icon code-box-code-action" @onclick="_ => codeExpand = !codeExpand">
                        <img alt="expand code" src="https://gw.alipayobjects.com/zos/rmsportal/wSAkBuJFbdxsosKKpqyq.svg" class="@(codeExpand ? "code-expand-icon-hide" : "code-expand-icon-show")">
                        <img alt="expand code" src="https://gw.alipayobjects.com/zos/rmsportal/OpROPHYqWmrMDBFMZtKF.svg" class="@(!codeExpand ? "code-expand-icon-hide" : "code-expand-icon-show")">
                    </span>
                </Unbound>
            </Tooltip>
            @DebugIcon
        </div>
    </section>
    <section class="highlight-wrapper @(codeExpand?"highlight-wrapper-expand":"")">
        <div class="highlight">
            <HighlightedCode Code="@Demo.Code" CanLoad="codeExpand" Language="html"></HighlightedCode>
        </div>
    </section>
    <style>
        @Demo.Style
    </style>
</div>

@inject InteropService InteropService
@inject DemoService demoService;
@inject ILanguageService lang;

@code {

    [Parameter]
    public string ComponentName { get; set; }

    [Parameter]
    public DemoItem Demo { get; set; }

    RenderFragment Component;

    RenderFragment DebugIcon;

    private bool codeExpand { get; set; }

    private bool _copied;

    protected string DemoId => $"components-{ComponentName.ToLower()}-demo-{Demo.Name}";

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        Component = demoService.GetShowCase(Demo.Type);

#if DEBUG
        DebugIcon =@<a href="mock?type=@(Demo.Type)" class="code-box-code-action" target="_blank">
            <Icon Type="bug" />
        </a>;
#endif
    }

    async Task Copy()
    {
        await InteropService.Copy(Demo.Code);
        _copied = true;
    }

    async Task AnchorClick()
    {
        await InteropService.JsInvokeAsync("window.eval", $"window.location.hash='{DemoId}'");
    }
}
